package com.lorry;

/**
 * 等待线程执行终止的 join 方法
 * interrupt
 *
 * for循环中断要中断主线程才会InterruptedException
 * 休眠中断对应线程和主线程都可以
 */
public class ThreadCommunication05 {

    public static void main(String[] args) throws InterruptedException {

          //线程one

        Thread threadOne = new Thread(new Runnable() {
              @Override
              public void run() {
                  System.out.println("threadOne begin run!");
//                  for (;;){
////                      System.out.println("111");
//                  }
                  try {
                      Thread.sleep(10000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
        });
          //获取主线程
        final Thread mainThread = Thread.currentThread();

        //线程two
        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                //休眠1s
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //中断主线程
                mainThread.interrupt();
                System.out.println("222");
            }
        });

        //启动子线程
        threadOne.start();

        //延迟1s启动线程
        threadTwo.start();

//        try{//等待线程one执行结束
            threadOne.join();

//        }catch (InterruptedException e){
//            System.out.println("main thread:"+e);
//        }

    }
}
